package com.ke.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ke.reggie.common.R;
import com.ke.reggie.entity.Category;
import com.ke.reggie.entity.Dish;
import com.ke.reggie.entity.Setmeal;
import com.ke.reggie.service.CategoryService;
import com.ke.reggie.service.DishService;
import com.ke.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: reggie_take_out
 * @BelongsPackage: com.ke.reggie.controller
 * @Author: ke_huifeng
 * @CreateTime: 2022-10-05  14:22
 * @Description: TODO
 * @Version: 1.0
 */

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R<String> saveCategory(@RequestBody Category category){

        categoryService.save(category);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){

        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo, categoryLambdaQueryWrapper);

        return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> deleteCategory(Long ids){
        categoryService.removeById(ids);

        return R.success("删除成功！");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功！");
    }

    @GetMapping("/list")
    public R<List<Category>> listCategory(Integer type){

        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(type != null, Category::getType, type);
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);

        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);
        return R.success(list);
    }
}
